<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">欢迎登录</div>
      <el-form :model="loginForm" :rules="rules" ref="login" label-width="0px" class="ms-content">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="输入账号">
            <el-button slot="prepend" icon="el-icon-user"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" style="margin-top: 25px">
          <el-input type="password" placeholder="登录密码" v-model="loginForm.password" @keyup.enter.native="handleLogin()">
            <el-button slot="prepend" icon="el-icon-lock"></el-button>
          </el-input>
        </el-form-item>
          <el-form-item style="width:100%;">
              <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
                  <span v-if="!loading">登 录</span>
                  <span v-else>登 录 中...</span>
              </el-button>
              <span>注：账号密码随便填写（ admin 是管理员，其他为普通用户）</span>
          </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
    export default {
    name: 'Login',  // 登录页面
    data(){
        return{
            loginForm: {
                username: "admin",
                password: "123456"
            },
            rules: {
                username: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
                password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
            },
            loading: false,
            redirect: undefined
        }
    },
    watch: {
        $route: {
            handler: function(route) {
                this.redirect = route.query && route.query.redirect;
            },
            immediate: true
        }
    },
    created() {

    },
    methods: {
        //处理登录方法
        handleLogin() {
            this.$refs.login.validate((valid) => {
                if (valid) {
                    this.loading = true
                    this.$store.dispatch('Login', this.loginForm)
                      .then(() => {
                          this.$message.success('登录成功');
                          localStorage.setItem('ms_username', this.loginForm.username);
                          this.$router.push({ path: this.redirect || "/" })
                      })
                      .catch(() => {
                          this.loading = false
                      })
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    this.loading = false
                    return false;
                }
            });
        }
    }
}
</script>

<style scoped>
.login-wrap {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url('../assets/img/bg3.png');
}
.ms-title {
  width: 100%;
  text-align: center;
  font-size: 22px;
  margin: 25px 0px 15px;
}
.ms-login {
  position: relative;
  width: 450px;
  height: 335px;
  max-width: 90%;
  margin: 275px auto;
  border-radius: 5px;
  background: white;
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  margin-top: 10%;
}
.login-btn button {
  width: 100%;
  height: 36px;
  text-align: center;
  margin-bottom: 10px;
}
</style>
